package com.umerclass.ui.anmo.one;

import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
import com.umerclass.AppDroid;
import com.umerclass.R;
import com.umerclass.bean.anmo.TeamGroupBean;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.okhttp.OkEntityListRequest;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.ui.anmo.adapter.MyFragmentPagerAdapter;
import com.umerclass.utils.Constants;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

public class MyTeamActivity extends BaseActivity {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    private List<TeamGroupBean> teamGroupBeans = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private MyFragmentPagerAdapter myFragmentPagerAdapter;

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_anmo_one_team;
    }

    @Override
    public void init() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.white));
    }

    @Override
    public void loadData() {
        OkEntityListRequest<TeamGroupBean> request = new OkEntityListRequest<>(R.id.myTeamGroup,
                Constants.myTeamGroup, TeamGroupBean.class);
        request.addParams("userId", AppDroid.getInstance().getUserOtherBean().getUserId());
        request.addHeadParams("userType",1);
        request.addHeadParams("userToken",AppDroid.getInstance().getUserOtherBean().getToken());
        requestOkhttpEntityList(request);
    }

    @OnClick({R.id.linear_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.myTeamGroup:
                teamGroupBeans = (List<TeamGroupBean>) t.getT();
                if (teamGroupBeans != null && teamGroupBeans.size() > 0) {
                    ArrayList<Fragment> fragments = new ArrayList<>();
                    for (int i = 0; i < teamGroupBeans.size(); i++) {
                        titles.add(teamGroupBeans.get(i).getName() +"("+
                                teamGroupBeans.get(i).getTotalNum()+"人)");
                        tab.addTab(tab.newTab().setText(teamGroupBeans.get(i).getName() +"("+
                                teamGroupBeans.get(i).getTotalNum()+"人)"));
                        int type  = teamGroupBeans.get(i).getBusinessType();
                        TeamFragment teamFragment = new TeamFragment();
                        teamFragment.setBusinessType(type);
                        fragments.add(teamFragment);
                    }
                    myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
                    myFragmentPagerAdapter.setFragments(fragments);
                    myFragmentPagerAdapter.setTitles(titles);
                    viewpager.setAdapter(myFragmentPagerAdapter);
                    tab.setupWithViewPager(viewpager);
                    viewpager.setCurrentItem(0);
                    viewpager.setOffscreenPageLimit(teamGroupBeans.size());
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

}
